package cn.tansanqinger.controller.topic;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.databind.ObjectMapper;

import cn.tansanqinger.service.topic.IQueryTopicService;
import cn.tansanqinger.service.topic.ITopicService;
import cn.tansanqinger.util.Tools;

@Controller
@RequestMapping("/admin/topic")
public class TopicQueryController {
	
	@Autowired
	private ITopicService service;
	@Autowired
	private IQueryTopicService qservice;
	
	@RequestMapping("/list5")
	public ModelAndView list(HttpServletRequest request, ModelAndView mv) {
		mv.setViewName("topic/list5");
		return mv;
	}
	
	@RequestMapping("/findListData5")
	@ResponseBody
	public String findListData(HttpServletRequest request) {
		try {
			return new ObjectMapper().writeValueAsString(service.selectMessage(request));
		} catch (Exception e) {}
		return null;
	}
	
	/**
	 * 添加信息
	 * @param request
	 * @param mv
	 * @return
	 */
	@RequestMapping("/goAdd5")
	public ModelAndView goAdd(HttpServletRequest request, ModelAndView mv) {
		mv.addObject("serialNumber", service.serialNumber());
		mv.setViewName("topic/goAdd5");
		return mv;
	}
	
	/**
	 * 编辑信息
	 * @param request
	 * @param mv
	 * @return
	 */
	@RequestMapping("/edit5")
	public ModelAndView edit(HttpServletRequest request, ModelAndView mv) {
		mv.addObject("goods", service.selectIdOne(Tools.isEmpty(request.getParameter("gid"))));
		mv.setViewName("topic/edit5");
		return mv;
	}
	
	/**
	 * 添加数据
	 * @param request
	 * @return
	 */
	@RequestMapping("/addMenu5")
	public @ResponseBody Map<String, Object> addMenu(HttpServletRequest request) {
		return service.add(request);
	}
	
	/**
	 * 数据删除
	 * @param request
	 * @return
	 */
	@RequestMapping("/delete5")
	@ResponseBody
	public Map<String, Object> delete(HttpServletRequest request){
		return service.delete(Tools.isEmpty(request.getParameter("gid")));
	}
	
	/**
	 * 修改信息
	 * @param request
	 * @return
	 */
	@RequestMapping("/modification5")
	@ResponseBody
	public Map<String, Object> modification(HttpServletRequest request){
		return qservice.modification(request);
	}
	
	@RequestMapping("/queryedit")
	public ModelAndView queryedit(HttpServletRequest request, ModelAndView mv) {
		mv.addObject("queryTopics", qservice.selectTIdOne(Tools.isEmpty(request.getParameter("id"))));
		mv.addObject("tid", Tools.isEmpty(request.getParameter("id")));
		mv.setViewName("topic/queryedit");
		return mv;
	}
}
